//
//  BRMInfoListView.m
//  BrezzeSaaS
//
//  Created by macos on 16.3.22.
//  Copyright © 2022 Brezze GWG. All rights reserved.
//

#import "BRMInfoListView.h"

@interface BRMInfoListView ()<QMUITextViewDelegate>
{
    QMUILabel *_titleL;
    UIView *_lineView;
    UIImageView *_leftImg;
    UIImageView *_moreImg;
}
@end

@implementation BRMInfoListView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setUI];
    }
    return self;
}

#pragma mark - func --
- (void)setIsEdit:(BOOL)isEdit {
    _isEdit = isEdit;
//    _textView.
}

- (void)setTitleStr:(NSString *)titleStr {
    _titleStr = titleStr;
    _titleL.text = titleStr;
}
- (void)setTitleStrColor:(UIColor *)titleStrColor {
    _titleStrColor = titleStrColor;
    _titleL.textColor = titleStrColor;
}

- (void)setLineHidde:(BOOL)lineHidde {
    _lineHidde = lineHidde;
    
    _lineView.hidden = lineHidde;
}

- (void)setPlaceholder:(NSString *)placeholder {
    _placeholder = placeholder;
    _textView.placeholder = placeholder;
}

- (void)setMoreImgHidde:(BOOL)moreImgHidde {
    _moreImgHidde = moreImgHidde;
    _moreImg.hidden = moreImgHidde;
}

- (void)setLeftImgStr:(NSString *)leftImgStr {
    _leftImgStr = leftImgStr;
    if (leftImgStr.length != 0) {
        _leftImg.image = UIImageMake(leftImgStr);
        _leftImg.hidden = NO;
        [_textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.offset(34);
        }];
    }else {
        _leftImg.hidden = YES;
        [_textView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.left.offset(16);
        }];
    }
}

- (void)setListType:(BRMShopInfoENUM)listType {
    _listType = listType;
    if (self.listType == BRMShopInf_phone) {
        self.textView.keyboardType = UIKeyboardTypeNumberPad;
    }

}

#pragma mark - textView delegate --
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height {
    
    CGFloat textView_h = height > 40.0 ? height : 40.0 ;
    NSLog(@"height = %.2f,textView_h = %.2f",height,textView_h);
    [_textView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(textView_h);
    }];
 
}

- (void)textViewDidChange:(UITextView *)textView {
    
    if (self.listType == BRMShopInf_name) {
        if (self.textView.text.length > 40) {
            self.textView.text = [self.textView.text substringToIndex:40];
        }
    }
    
    if (self.listType == BRMShopInf_phone) {
        if (self.textView.text.length > 15) {
            self.textView.text = [self.textView.text substringToIndex:15];
        }
//        if (self.textView.text.length < 8) {
//            return;
//        }
    }
    
    if (self.textChangBlock) {
        self.textChangBlock(_textView.text);
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    
    if (self.listType == BRMShopInf_ratio) {
        // 分成单独处理
        NSLog(@"[%@][%@]",textView.text,text);
        NSString *sum = [NSString stringWithFormat:@"%@%@",textView.text,text];
        if (sum.floatValue > 100) return NO;
        
    }
    return YES;
}

#pragma mark - UI --
- (void)setUI {
    
    QMUILabel *title = [BRToolsView br_label:@"Status" textColor:kColorTitle font:[UIFont fontM:14]];
    [self addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(12);
        make.left.offset(16);
    }];
    _titleL = title;
    
    UIView *line = [BRToolsView getLineView];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0.5);
        make.left.offset(16);
        make.right.offset(0);
        make.bottom.offset(0);
    }];
    _lineView = line;
    
    QMUITextView *textView = [[QMUITextView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth - 64, 30)];
    textView.font = [UIFont br_fontM:16];
    textView.delegate = self;
    textView.scrollEnabled = NO;
    textView.text = @"";
    textView.tintColor = kColorMain;
    textView.textColor = UIColorText1;
    textView.placeholderColor = UIColorMake(210, 216, 226);
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(34);
        make.left.offset(16);
        make.right.offset(-16);
        make.bottom.offset(-4);
        make.height.mas_equalTo(40);
    }];
    textView.contentInset = UIEdgeInsetsMake(0, -3, 0, 0);//与标题对齐
    _textView = textView;
//    textView addt
    
    UIImageView *lImg = [[UIImageView alloc] init];
    lImg.hidden = YES;
    [self addSubview:lImg];
    [lImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(12);
        make.left.offset(16);
        make.centerY.equalTo(textView).offset(0);
    }];
    _leftImg = lImg;
    
    UIImageView *rightImg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_right_garyShallow6")];
    rightImg.hidden = YES;
    [self addSubview:rightImg];
    [rightImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(6);
        make.height.mas_equalTo(10);
        make.centerY.equalTo(title.mas_centerY).offset(0);
        make.right.offset(-16);
    }];
    _moreImg = rightImg;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

@implementation BRMInfoListMoreView

- (void)toOpenOtp {
  
    if (self.toOtpBlock) {
        self.toOtpBlock();
    }
}

#pragma mark - UI --
- (void)setUI {
    [super setUI];
    
    [self addSubview:self.otpView];
    [self.otpView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.textView.mas_centerY).offset(-2);
        make.left.offset(16);
        make.height.mas_equalTo(40);
    }];
    UIControl *otpC = [[UIControl alloc] init];
    [otpC addTarget:self action:@selector(toOpenOtp) forControlEvents:UIControlEventTouchUpInside];
    [self.otpView addSubview:otpC];
    [otpC mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    
    [self addSubview:self.openImg];
    [self.openImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.textView.mas_centerY).offset(-2);
        make.right.offset(-16);
        make.size.mas_equalTo(CGSizeMake(8, 5));
    }];
    [self addSubview:self.moreL];
    [self.moreL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.textView.mas_centerY).offset(0);
        make.right.offset(-16);
    }];
}

- (void)br_showOtpView {
    self.otpView.hidden = NO;
    
    [self.textView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(34);
        make.left.equalTo(self.otpView.mas_right).offset(10);
        make.right.offset(-16);
        make.bottom.offset(-4);
        make.height.mas_equalTo(40);
    }];
    
}

- (void)br_showMoreLabel {
    self.moreL.hidden = NO;
}

- (void)br_showOpenImg {
    self.openImg.hidden = NO;
}

- (UIImageView *)openImg {
    
    if (_openImg == nil) {
        _openImg = [[UIImageView alloc] initWithImage:UIImageMake(@"arrow_triangle_Black8")];
        _openImg.hidden = YES;
    }
    
    return _openImg;
}

- (QMUILabel *)moreL {
    
    if (_moreL == nil) {
        _moreL = [BRToolsView br_label:@"%" textColor:kColorTitle font:[UIFont br_fontS:14]];
        _moreL.hidden = YES;
    }
    return _moreL;
}

- (BROtpView *)otpView {
    
    if (_otpView == nil) {
        _otpView = [[BROtpView alloc] init];
        _otpView.hidden = YES;
    }
    return _otpView;
}

@end
